package top.yh.service;

import top.yh.model.Customer;

/**
 * 编写业务类
 */
public class CustomerList {
    //定义客户容器
    private Customer[] customers;
    //定义客户数量
    private int total;
    //给容器初始化
    public CustomerList(int length){
        customers = new Customer[length];
    }

    //客户添加操作
    public boolean add(Customer customer){
       //超过数组容器范围，添加失败
       if(total>=customers.length){
           return false;
       }
       customers[total++] = customer;
       return  true;
    }
    //根据下标查找客户
    public Customer getCustomer(int index){
        if(index>=total){
            return null;
        }
        Customer customer = customers[index];
        return customer;
    }
    //修改客户
    public boolean modify(int index,Customer customer){
        if(index>=total){
            return false;
        }
        customers[index] = customer;
        return true;
    }
    //删除客户
    public boolean delete(int index){
        if(index>=total){
            return false;
        }
        for(int i=index;i<total-1;i++){
            customers[i] = customers[i+1];
        }
        customers[--total] = null;
        return true;
    }
    //查询客户
    public Customer[] getCustomersList(){
        Customer[] customerList = new Customer[total];
        for(int i=0;i<customerList.length;i++){
            customerList[i] = customers[i];
        }
        return customerList;
    }
    //返回客户数量
    public int getTotal(){
        return this.total;
    }
}
